package com.mihu.stmall.manager.service.impl;

import com.mihu.stmall.manager.entiy.Admin;
import com.mihu.stmall.manager.mapper.AdminMapper;
import com.mihu.stmall.manager.service.AdminService;
import com.mihu.stmall.manager.util.MD5Util;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service("AdminService")
@AllArgsConstructor
public class AdminServiceImpl implements AdminService {

    //Spring4.x支持构造方法注入  提供一个有参构造 不需要任何注解
    private AdminMapper adminMapper;

//    public AdminServiceImpl(AdminMapper adminMapper){
//        this.adminMapper=adminMapper;
//    }

    @Override
    public Admin login(Admin admin) throws Exception {
        //参数验证
        if (admin.getUsername() ==null ||
        admin.getUsername().length()<3 ||
        admin.getUsername().length()>16){
            throw new Exception("管理员名必须是3~16位字符串");
        }

        if (admin.getPassword() ==null ||
                admin.getPassword().length()<3 ||
                admin.getPassword().length()>16){
            throw new Exception("密码必须是3~16位字符串");
        }

        //根据用户名查询
        Admin sAdmin =adminMapper.selectByUsername(admin.getUsername());
        if (sAdmin ==null){
            throw new Exception("用户名不存在");
        }

//验证密码
        String password = admin.getPassword();
        password = MD5Util.getMD5(password+sAdmin.getSalt());

        if (! password.equals(sAdmin.getPassword())){
            throw new Exception("密码错误");
        }

        if (sAdmin.getStatus() ==1){
            throw new Exception("用户被禁用");
        }
        return sAdmin;
    }

    @Override
    public Admin modifyPwd(Integer id, String oldPassword, String newPassword, String rePassword) throws Exception {

        //验证参数
        if (oldPassword ==null ||
        oldPassword.length()<3 ||
        oldPassword.length()>16){
            throw new Exception("旧密码格式错误");
        }
        if (newPassword ==null ||
                newPassword.length()<3 ||
                newPassword.length()>16){
            throw new Exception("新密码格式错误");
        }

        if (! newPassword.equals(rePassword)){
            throw new Exception("两次输入密码不一致");
        }

        //取出用户数据
        Admin admin =adminMapper.selectById(id);

        String md5Pwd = MD5Util.getMD5(oldPassword+admin.getSalt());

        if (! admin.getPassword().equals(md5Pwd)){
            throw new Exception("旧密码错误");
        }
        admin.setPassword(MD5Util.getMD5(newPassword+admin.getSalt()));
        adminMapper.update(admin);
        return admin;
    }

    @Override
    public Admin edit(Admin admin) throws Exception {
        return null;
    }

    @Override
    public int add(Admin admin) throws Exception {
        if (admin.getUsername()==null ||
        admin.getUsername().length()<3 ||
        admin.getUsername().length()>16){
            throw new Exception("用户名不能为空或格式错误");
        }

        if (admin.getPassword()==null){
            throw new Exception("密码不能为空");
        }

        if (
        admin.getPhone().length()!=11){
            throw new Exception("电话号码格式错误");
        }

        admin.setSalt(admin.getUsername());

        String md5Pwd = MD5Util.getMD5(admin.getPassword()+admin.getSalt());

        admin.setPassword(md5Pwd);
        return adminMapper.insert(admin);
    }

    @Override
    public int remove(Integer id) throws Exception {
        if (adminMapper.selectById(id) !=null){
            return adminMapper.delete(id);
        }else {
            throw new Exception("该管理员不存在 id不存在");
        }

    }
}
